/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA

Dialog {
    id: personalprofilerect
    width: 550
    height: 400
    color: "#5d8eac"
    border.width: 0
    border.color: "#000000"
    label:  "Personal Profile"

    property string datacloudurl: ""
    property string username: ""
    property string password: ""
    property string karpoid: ""

    property string firstname: ""
    property string lastname: ""
    property string hometown: ""
    property string phone: ""
    property string alias: ""


    property string showmyname: "0"
    property string showmyhometown: "0"
    property string showmyphone: "0"

    property string _showmyname: "0"
    property string _showmyhometown: "0"
    property string _showmyphone: "0"

    signal profileOk
    signal profileCancel

    function getAllData()
    {
        var temp = {}
        temp["username"]=username;
        temp["password"]=password;
        temp["karpoid"]=karpoid;

        temp["firstname"]=firstname;
        temp["lastname"]=lastname;
        temp["hometown"]=hometown;
        temp["phone"]=phone;
        temp["alias"]=aliasinput.text;

        return temp;
    }

    function getPrivacyData()
    {
        var temp = {}
        temp["showname"] = showmyname
        temp["showphone"] = showmyphone
        temp["showhometown"] = showmyhometown

        return temp

    }

    function getData()
    {

        var temp = {}
        temp["firstname"]=firstnameinput.text;
        temp["lastname"]=lastnameinput.text;
        temp["hometown"]=hometowninput.text;
        temp["phone"]=phoneinput.text;
        temp["alias"]=aliasinput.text;


        return temp;
    }

    function setPrivacySettings(privacysettings)
    {
        showmyname = privacysettings["showname"]
        showmyphone = privacysettings["showphone"]
        showmyhometown = privacysettings["showhometown"]

        _showmyname = showmyname
        _showmyphone = showmyphone
        _showmyhometown = showmyhometown
    }


    function setData(userdata)
    {

        console.log("setData:\r\n-----------------")
        try
        {
            personalprofilerect.username = userdata["username"]
            personalprofilerect.password = userdata["password"]
            personalprofilerect.karpoid = userdata["karpoid"]


            firstnameinput.text = userdata["firstname"]
            lastnameinput.text = userdata["lastname"]
            hometowninput.text = userdata["hometown"]
            phoneinput.text = userdata["phone"]
            aliasinput.text = userdata["alias"]

            personalprofilerect.firstname = firstnameinput.text
            personalprofilerect.lastname = lastnameinput.text
            personalprofilerect.hometown = hometowninput.text
            personalprofilerect.phone = phoneinput.text
            personalprofilerect.alias = aliasinput.text

        }
        catch(e)
        {
            console.log("PersonalProfile::setData exception: " + e)
        }

        personalprofilerect.backupData()

    }

    function restoreData()
    {
        firstnameinput.text = firstname
        lastnameinput.text = lastname
        hometowninput.text = hometown
        phoneinput.text = phone
        aliasinput.text = alias
        showmyname = _showmyname
        showmyphone = _showmyphone
        showmyhometown = _showmyhometown

    }

    function backupData()
    {
        firstname = firstnameinput.text
        lastname = lastnameinput.text
        hometown = hometowninput.text
        phone = phoneinput.text
        alias = aliasinput.text

        _showmyname = showmyname
        _showmyphone = showmyphone
        _showmyhometown = showmyhometown

    }

    function saveprofilecb(response)
    {
        console.log("\r\nPersonalProfile::saveprofilecb => " + response)
        if (response.isError)
        {
            messagebox.text = response.errorString
        }
        else
        {
            // login ok
            messagebox.visible = false
            personalprofilerect.backupData()
            personalprofilerect.profileOk()
        }
    }


    onOk:
    {
        console.log("\r\nPersonalProfile::onOk--------------------------\r\n")

        messagebox.text = "Saving the profile..."
        messagebox.visible = true

        var personalprofile = new DCDATA.DCPersonalProfile();
        personalprofile.karpoid = personalprofilerect.karpoid
        personalprofile.password = personalprofilerect.password
        personalprofile.username = personalprofilerect.username
        personalprofile.firstname = firstnameinput.text
        personalprofile.lastname = lastnameinput.text
        personalprofile.hometown = hometowninput.text
        personalprofile.phone = phoneinput.text
        personalprofile.alias = aliasinput.text
        personalprofile.showname = (personalprofilerect.showmyname == "1")
        personalprofile.showphone = (personalprofilerect.showmyphone == "1")
        personalprofile.showhometown = (personalprofilerect.showmyhometown == "1")

        DCDATA.send(personalprofile, datacloudurl, "savepersonalprofile", saveprofilecb)


    }

    onCancel:
    {
        // restore the data
        personalprofilerect.restoreData()
        personalprofilerect.profileCancel()
    }



    property int labelfontpixesize: 14
    property int labeltopmargin: 12
    property int inputboxheight: 26
    property int inputboxtopmargin: 2
    property int labelWidth: 90


    Rectangle
    {
        id: firstnamerect
        color: "#00000000"
        height: personalprofilerect.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: personalprofilerect.titlerect.bottom
        anchors.topMargin: personalprofilerect.inputboxtopmargin

        Text {
            id: firstnameid
            width: personalprofilerect.labelWidth
            text: "First name"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: firstnamerect.verticalCenter
            font.pixelSize: personalprofilerect.labelfontpixesize
        }

        Rectangle {
            id: firstnameinputrectangle
            height: personalprofilerect.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: firstnameid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: firstnameinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: personalprofilerect.labelfontpixesize
            }
        }

    }

    Rectangle
    {
        id: lastnamerect
        color: "#00000000"
        height: personalprofilerect.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: firstnamerect.bottom
        anchors.topMargin: personalprofilerect.inputboxtopmargin

        Text {
            id: lastnameid
            width: personalprofilerect.labelWidth
            text: "Last name"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: lastnamerect.verticalCenter
            font.pixelSize: personalprofilerect.labelfontpixesize
        }

        Rectangle {
            id: lastnameinputrectangle
            height: personalprofilerect.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: lastnameid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: lastnameinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: personalprofilerect.labelfontpixesize
            }
        }

    }

    Rectangle
    {
        id: hometownrect
        color: "#00000000"
        height: personalprofilerect.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: lastnamerect.bottom
        anchors.topMargin: personalprofilerect.inputboxtopmargin

        Text {
            id: hometownid
            width: personalprofilerect.labelWidth
            text: "Hometown"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: hometownrect.verticalCenter
            font.pixelSize: personalprofilerect.labelfontpixesize
        }

        Rectangle {
            id: hometowninputrectangle
            height: personalprofilerect.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: hometownid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: hometowninput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: personalprofilerect.labelfontpixesize
            }
        }

    }

    Rectangle
    {
        id: phonerect
        color: "#00000000"
        height: personalprofilerect.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: hometownrect.bottom
        anchors.topMargin: personalprofilerect.inputboxtopmargin

        Text {
            id: phoneid
            width: personalprofilerect.labelWidth
            text: "Phone #"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: phonerect.verticalCenter
            font.pixelSize: personalprofilerect.labelfontpixesize
        }

        Rectangle {
            id: phoneinputrectangle
            height: personalprofilerect.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: phoneid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: phoneinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: personalprofilerect.labelfontpixesize
            }
        }

    }


    Rectangle
    {
        id: aliasrect
        color: "#00000000"
        height: personalprofilerect.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: phonerect.bottom
        anchors.topMargin: personalprofilerect.inputboxtopmargin

        Text {
            id: aliasid
            width: personalprofilerect.labelWidth
            text: "Alias"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: aliasrect.verticalCenter
            font.pixelSize: personalprofilerect.labelfontpixesize
        }

        Rectangle {
            id: aliasinputrectangle
            height: personalprofilerect.inputboxheight
            color: "#ffffff"
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.left: aliasid.right
            anchors.leftMargin: 0
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            border.color: "#000901"

            TextInput {
                id: aliasinput
                text: ""
                anchors.leftMargin: 3
                anchors.fill: parent
                selectionColor: "#316ac5"
                font.pixelSize: personalprofilerect.labelfontpixesize
            }
        }
    }

    Rectangle
    {

        id: privacyrect
        color: "#00000000"
        height: personalprofilerect.inputboxheight
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: aliasrect.bottom
        anchors.topMargin: personalprofilerect.inputboxtopmargin

        Text {
            id: privacyid
            width:personalprofilerect.labelWidth
            text: "Show my"
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: privacyrect.verticalCenter
            font.pixelSize: personalprofilerect.labelfontpixesize
        }

        CheckBox
        {
            id: showmynamecheckbox
            width: 70
            label: "name"
            textfontpixelsize: 12
            selected: (personalprofilerect.showmyname=="1")
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.left: privacyid.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top

            onSelectionchanged:
            {
                isSelected?personalprofilerect.showmyname="1":personalprofilerect.showmyname="0"
            }
        }

        CheckBox
        {
            id: showmyphonecheckbox
            width: 70
            label: "phone"
            textfontpixelsize: 12
            selected: (personalprofilerect.showmyphone=="1")
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.left: showmynamecheckbox.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top

            onSelectionchanged:
            {
                isSelected?personalprofilerect.showmyphone="1":personalprofilerect.showmyphone="0"
            }
        }

        CheckBox
        {
            id: showmynhometowncheckbox
            width: 95
            label: "hometown"
            textfontpixelsize: 12
            selected: (personalprofilerect.showmyhometown=="1")
            anchors.leftMargin: 0
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.left: showmyphonecheckbox.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top

            onSelectionchanged:
            {
                isSelected?personalprofilerect.showmyhometown="1":personalprofilerect.showmyhometown="0"
            }
        }


    }

    function init()
    {
        var arr = []
        personalprofilerect.addMenuItems(arr,true)

    }

    onMenuSelection:
    {
    }


    MessageBox
    {
        id: messagebox
    }

}
